import {
	defineStore
} from 'pinia'
import {
	ref,
	computed
} from 'vue'
import {
	orderMsg
} from '@/stores'

export default defineStore('order', () => {
	const orderList = ref([])
	const msgStore = orderMsg()

	//订单添加
	const putOrder = (item) => {
		orderList.value.unshift({
			id: Date.now().toString(36) + Math.random().toString(36).substring(2),
			...item
		})
		msgStore.setCurrentOrder(orderList.value[0])
	}
	//重置
	const resetOrderList = () => {
		orderList.value = []
	}
	//修改status值
	const updateOrderStatus = (key, item) => {
		orderList.value[key] = {
			...orderList.value[key],
			status: item
		}
	}
	//替换对象
	const updateOrderReason = (index, reason) => {
		if (index !== -1) {
			orderList.value[index] = {
				...orderList.value[index],
				reason: reason
			}
		}
	}
	//订单删除(根据下标)
	const deleteOrderByIndex = (index) => {
		if (index >= 0 && index < orderList.value.length) {
			orderList.value.splice(index, 1)
		}
	}

	return {
		orderList,
		putOrder,
		resetOrderList,
		updateOrderStatus,
		updateOrderReason,
		deleteOrderByIndex
	}
}, {
	persist: true
})